package com.lyw.gulimall.product.web;

import com.lyw.gulimall.product.entity.CategoryEntity;
import com.lyw.gulimall.product.service.CategoryService;
import com.lyw.gulimall.product.vo.front.Catalog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取一级分类用于展示
     */
    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    /**
     * 固定的格式返回前端需要的三级分类id为k，二级分类以及关联的三级分类为v的 map，用于前端遍历
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<Long, List<Catalog2Vo>> getCatalogJson() {
        Map<Long, List<Catalog2Vo>> result = categoryService.getCatalogJson();
        return result;
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}
